home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / answrbok / 6_7.lha / 6_7 / 6_7tst.c < prev    next >
Text File  |  1993-08-08  |  2KB  |  110 lines

  1. * Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
  2. * The C++ Answer Book */
  3. * Tony Hansen */
  4. * All rights reserved. */
  5. * Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
  6. * The C++ Answer Book */
  7. * Tony Hansen */
  8. * All rights reserved. */
  9. / Exercise 6.7
  10. / print out the conversions and
  11. / values for each expression
  12. include <stream.h>
  13.  
  14. truct X
  15.  
  16.    int i;
  17.  
  18.    X(int in)
  19.    {
  20. cout << "converting int " << in << " to X\n";
  21. i = in;
  22.    }
  23.  
  24.    operator+(int in)
  25.    {
  26. cout << "adding X " << i << " + int " << in << "\n";
  27. int ret = i + in;
  28. cout << "op+(X,int) returns " << ret << "\n";
  29. return ret;
  30.    }
  31. ;
  32.  
  33. truct Y
  34.  
  35.    int i;
  36.  
  37.    Y(X Xin)
  38.    {
  39. cout << "converting X " << Xin.i << " to Y\n";
  40. i = Xin.i;
  41.    }
  42.  
  43.    operator+(X Xin)
  44.    {
  45. cout << "adding Y " << i << " to X " <<
  46.     Xin.i << "\n";
  47. int ret = i + Xin.i;
  48. cout << "op+(Y,X) returns " << ret << "\n";
  49. return ret;
  50.    }
  51.  
  52.    operator int()
  53.    {
  54. cout << "converting Y " << i <<
  55.     " to int\n";
  56. return i;
  57.    }
  58. ;
  59.  
  60. X operator* (X Xin, Y Yin)
  61.  
  62.    cout << "multiplying X " << Xin.i << " times Y " <<
  63. Yin.i << "\n";
  64.    X Xout(Xin.i * Yin.i);
  65.    cout << "op*(X, Y) returns " << Xout.i << "\n";
  66.    return Xout;
  67.  
  68.  
  69. nt f(X Xin)
  70.  
  71.    cout << "calling f(X = " << Xin.i << ")\n";
  72.    return Xin.i;
  73.  
  74.  
  75. X x = 1;
  76.  y = x;
  77. nt i = 2;
  78.  
  79. ain()
  80.  
  81.    int ret = i + 10;
  82.    cout << "i + 10 = " << ret << "\n";
  83.  
  84.    ret = y + 10;
  85.    cout << "y + 10 = " << ret << "\n";
  86.  
  87.    ret = y + 10 * y;
  88.    cout << "y + 10 * y = " << ret << "\n";
  89.  
  90.    ret = x + y + i;
  91.    cout << "x + y + i = " << ret << "\n";
  92.  
  93.    ret = x * x + i;
  94.    cout << "x * x + i = " << ret << "\n";
  95.  
  96.    ret = f(7);
  97.    cout << "f(7) = " << ret << "\n";
  98.  
  99. /  illegal conversion
  100. /  ret = f(y);
  101. /  cout << "f(y) = " << ret << "\n";
  102.  
  103.    ret = y + y;
  104.    cout << "y + y = " << ret << "\n";
  105.  
  106.    ret = 106 + y;
  107.    cout << "106 + y = " << ret << "\n";
  108.    return 0;
  109.  
  110.